package cn.lovezaoan.hpcenter.manager
{
    import flash.events.Event;
    import flash.html.HTMLLoader;
    import mx.collections.ArrayList;
    import cn.lovezaoan.hpcenter.parse.ExtractCfgManager;
    import cn.lovezaoan.hpcenter.parse.ExtractSiteCfg;
    import cn.lovezaoan.hpcenter.parse.ExtractTypeCfg;
    import cn.lovezaoan.hpcenter.parse.HtmlExtract;
    
    /**
     * PortalManager类
     * 依据类型返回Portal
     * 列表
     * @author yuyuzhu
     *
     */
    public class PortalManager
    {
        
        public static function getPanels():void
        {
        
        }
        
        public function PortalManager(portalType:int, panelType:String, caller:Object,
                                      parentType:String)
        {
            this.portalType = portalType;
            this.panelType = panelType;
            this.caller = caller;
            this.parentType = parentType;
        }
        
        public var htmlExtract:HtmlExtract;
        
        /**
         * 调用PortalManager类中方法的对象
         */
        private var caller:Object;
        
        private var panelType:String;
        
        private var parentType:String;
        
        private var portalType:int;
        
        private var siteCfgs:ArrayList;
        
        /**
         * 依据portal类型请求Message列表。<br>
         * 注意：因为处理中涉及到读取html页面，所以这是一个异步的请求。请求结果通过回调函数callBack返回。
         * @param portalType
         * @return
         *
         */
        public function requestMessagesByTypeAndAsyncResponse():void
        {
            var extractCfgManager:ExtractCfgManager = ExtractCfgManager.getExtractCfgManager();
            var typeCfg:ExtractTypeCfg = extractCfgManager.getCfgByType(portalType);
            if (typeCfg != null)
            {
                siteCfgs = typeCfg.siteCfgs;
                getMessageListBySiteCfgs();
            }
        }
        
        private function checkUrlValidationAndModify(url:String, event:Event):String
        {
            var res:String = url;
            if (url != null && url.indexOf("http://") == -1)
            {
                res = (event.target as HTMLLoader).location + url;
            }
            
            return res;
        }
        
        private function extractHtml(event:Event):ArrayList
        {
            if (siteCfgs != null && siteCfgs.length > 0)
            {
                // 通过事件对应的HTMLLoader对象的location获取对应的ExtractSiteCfg
                var curSiteCfg:ExtractSiteCfg = findCurrentSiteCfg(event);
                
                if (curSiteCfg != null)
                {
                    var propertyRules:ArrayList = curSiteCfg.propertyRules;
                    var propertyNames:ArrayList = curSiteCfg.propertyNames;
                    var htmlResults:ArrayList = htmlExtract.extractMultiple(propertyRules);
                    var msg:Message;
                    var msgs:ArrayList = new ArrayList();
                    var proName:String;
                    if (htmlResults != null && htmlResults.length > 0)
                    {
                        var propertyLen:int = propertyRules.length;
                        var res4Property:ArrayList;
                        var htmlElement:Object;
                        // 首先，按每个元数据读取到的记录数循环（每个元数据读取到的数据数量是一样的）
                        var msgLen:int = htmlResults.getItemAt(0).length;
                        for (var i:int = 0; i < msgLen; i++)
                        {
                            if ("blog" == this.panelType)
                            {
                                msg = new BlogMessage();
                            }
                            else
                            {
                                msg = new Message();
                            }
                            
                            msgs.addItem(msg);
                            // 其次，按元数据个数横向循环，依次设置msg对象的各个属性
                            for (var j:int = 0; j < propertyLen; j++)
                            {
                                res4Property = htmlResults.getItemAt(j) as ArrayList;
                                proName = propertyNames.getItemAt(j).toString();
                                htmlElement = res4Property.getItemAt(i);
                                if (Portal.PANEL_BLOG == this.panelType && msg is BlogMessage && "description" == proName)
                                {
                                    msg["blogContent"] = htmlElement.innerHTML;
                                }
                                else
                                {
                                    if ("title" == proName)
                                    {
                                        if (Portal.PANEL_FLASH == this.panelType)
                                        {
                                            msg["url"] = checkUrlValidationAndModify(htmlElement.src, event);
                                        }
                                        else
                                        {
                                            msg["url"] = checkUrlValidationAndModify(htmlElement.href, event);
                                            msg[proName] = htmlElement.innerHTML;
                                        }
                                    }
                                    else
                                    {
                                        msg[proName] = htmlElement.innerHTML;
                                    }
                                }
                                
                            }
                        }
                        
                        if (msgs.length > 0)
                        {
                            this.caller.handleMessages(msgs, this.portalType, this.parentType);
                        }
                    }
                }
            }
            
            return msgs;
        }
        
        private function findCurrentSiteCfg(event:Event):ExtractSiteCfg
        {
            var url4Event:String = (event.target as HTMLLoader).location;
            var tmpSiteCfg:ExtractSiteCfg = null;
            var curSiteCfg:ExtractSiteCfg = null;
            for (var i:int = 0; i < siteCfgs.length; i++)
            {
                tmpSiteCfg = siteCfgs.getItemAt(i) as ExtractSiteCfg;
                if (tmpSiteCfg.url == url4Event)
                {
                    curSiteCfg = tmpSiteCfg;
                    break;
                }
            }
            
            return curSiteCfg;
        }
        
        /**
         * 依据 一个siteCfg,返回对应的messageList
         * @param htmlResults
         * @return
         *
         */
        private function getMessageListBySiteCfg(siteCfg:ExtractSiteCfg):void
        {
            var messageList:ArrayList;
            var rulsList:ArrayList;
            htmlExtract = new HtmlExtract(siteCfg.url);
            htmlExtract.html.addEventListener(Event.COMPLETE, extractHtml);
        }
        
        /**
         * 依据 siteCfgs获取MessageList
         * @param siteCfgs
         * @return
         *
         */
        private function getMessageListBySiteCfgs():void
        {
            var messageList:ArrayList = new ArrayList();
            if (siteCfgs !=null && siteCfgs.length > 0)
            {
                var siteCfg:ExtractSiteCfg;
                for (var j:int = 0; j < siteCfgs.length; j++)
                {
                    siteCfg = siteCfgs.getItemAt(j) as ExtractSiteCfg;
                    getMessageListBySiteCfg(siteCfg);
                    siteCfg = null;
                }
            }
        }
    }
}